﻿using System;
using System.Threading;
using System.Timers;

public class AlarmClock
{
    public event Action? Tick;
    public event Action? Alarm;

    private System.Timers.Timer _timer;
    private DateTime _alarmTime;

    public AlarmClock(DateTime alarmTime)
    {
        _alarmTime = alarmTime;
        _timer = new System.Timers.Timer(1000); // 每秒触发一次
        _timer.Elapsed += OnTick; // 绑定事件
    }

    public void Start()
    {
        Console.WriteLine("闹钟已启动...");
        _timer.Start();
    }

    private void OnTick(object? sender, ElapsedEventArgs e)
    {
        Tick?.Invoke();

        if (DateTime.Now >= _alarmTime)
        {
            _timer.Stop();
            Alarm?.Invoke();
        }
    }
}

class Program
{
    static void Main()
    {
        DateTime alarmTime = DateTime.Now.AddSeconds(5); // 5 秒后响铃
        AlarmClock alarmClock = new AlarmClock(alarmTime);

        alarmClock.Tick += () => Console.WriteLine("嘀嗒...");
        alarmClock.Alarm += () => Console.WriteLine("时间到了,响铃");

        alarmClock.Start();
        Console.ReadLine();
    }
}
